package com.zretc.controller;

import java.util.Arrays;
import java.util.List;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zretc.entity.Users;

@RestController
@RequestMapping("api")
public class UsersRestFulController {
	
	// http://xxx/spring/api/user/1
	// /1 如何传递给这个方法
	@GetMapping(value = "user/{id}", produces = "application/json;charset=utf-8")
	public Users listUsersById(@PathVariable("id") int id) {
		System.out.println("id :" + id);	
		//以下的数据是根据id调用service,从而获取数据库表记录		
		Users u1 = new Users();
		u1.setId(1);
		u1.setUsername("小啊");
		return u1; //测试结果而已
	}
	
	//@RequestBody Users user 向控制器提供一个参数，要求json格式的对象 user
	@PostMapping(value = "user", consumes = "application/json",produces = "application/json;charset=utf-8")
	public Users listUsers(@RequestBody Users user) {
		System.out.println("id :" + user);		
		//调用service,完成增加操作，返回结果值		
		return user; //测试结果而已
	}
	
	
	
	
	@GetMapping(value = "user", produces = "application/json;charset=utf-8")
	public List<Users> listUsers() {
		Users u1 = new Users();
		u1.setId(1);
		u1.setUsername("小啊");
		

		Users u2 = new Users();
		u2.setId(2);
		u2.setUsername("小2");
		
		return Arrays.asList(u1,u2); //测试结果而已
	}
	
	
	@GetMapping(value = "user1", produces = "application/json;charset=utf-8")
	public List<Users> listUsers1(String keywords) {
		//keywords是模糊查询的关键字
		Users u1 = new Users();
		u1.setId(1);
		u1.setUsername("小啊");
		

		Users u2 = new Users();
		u2.setId(2);
		u2.setUsername("小2");
		
		return Arrays.asList(u1,u2); //测试结果而已
	}
	
	

	
}
